package cn.com.ddapp.ddm.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.com.ddapp.ddm.R;


/**
 * Created by caopenghui on 2016-08-06.
 */
public class ImageViewUpLoad extends RelativeLayout implements View.OnClickListener{

    public static final int TYPE_ADD = 1;//增加图片
    public static final int TYPE_DELETE = 2;//删除图片

    public static final int PIC_ONE = 1;//增加图片
    public static final int PIC_TWO = 2;//增加图片
    public static final int PIC_THREE = 3;//增加图片


    private RelativeLayout rl_img1,rl_img2,rl_img3;
    private ImageView iv_img1,iv_img2,iv_img3;
    private ImageView iv_clear_img1,iv_clear_img2,iv_clear_img3;
    private ImageView iv_add_img1,iv_add_img2,iv_add_img3;
    private Integer goodsId;


    private List<Bitmap> bitmapList = new ArrayList<>();
    private OnPicAddAndDel onPicAddAndDel;
    private static Handler handler ;
    private Activity activity;

    private int index;//当前操作第几张图
    private int type;//当前是增加图片还是减少图片


    public ImageViewUpLoad(Context context) {
        super(context);
        if(handler==null){

            handler = new Handler(Looper.getMainLooper()){
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == 1 && msg.obj != null) {
//                        int index = msg.arg1;//处理的是第几张图片 1,2,3
//                        int type = msg.arg2;//处理的是增加图片还是减少图片  1为增加 2为减少
                        Bitmap bitmap = (Bitmap)msg.obj;
                        if(index==PIC_ONE&&type==TYPE_ADD){//增加第一张
                            if(bitmapList.size()>=1){
                                bitmapList.remove(0);
                            }
                            bitmapList.add(0,bitmap);
                            iv_img1.setImageBitmap(bitmap);
                            iv_add_img1.setVisibility(View.GONE);
                        }else if(index==PIC_ONE&&type==TYPE_DELETE){//减少第一张
                            bitmapList.remove(0);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==1){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==0){
                                iv_add_img1.setVisibility(View.VISIBLE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }else if(index==PIC_TWO&&type==TYPE_ADD){//增加第二张
                            if(bitmapList.size()==0){
                                bitmapList.add(bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            } else if(bitmapList.size()==1){
                                bitmapList.add(bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==2){
                                bitmapList.remove(1);
                                bitmapList.add(1,bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==3){
                                bitmapList.remove(1);
                                bitmapList.add(1,bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }
                        }else if(index==PIC_TWO&&type==TYPE_DELETE){//减少第二张
                            bitmapList.remove(1);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==1){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }else if(index==PIC_THREE&&type==TYPE_ADD){//增加第三张
                            if(bitmapList.size()==0){
                                bitmapList.add(bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            } else if(bitmapList.size()==1){
                                bitmapList.add(bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==2){
                                bitmapList.remove(2);
                                bitmapList.add(2,bitmap);
                                iv_img3.setImageBitmap(bitmapList.get(2));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }else if(bitmapList.size()==3){
                                bitmapList.remove(2);
                                bitmapList.add(2,bitmap);
                                iv_img3.setImageBitmap(bitmapList.get(2));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }
                        }else if(index==PIC_THREE&&type==TYPE_DELETE){//减少第三张
                            bitmapList.remove(2);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            };

        }
    }

    public  Handler getMyHandler() {
        return handler;
    }

    public ImageViewUpLoad(Context context, AttributeSet attrs ) {
        super(context, attrs);
        //加载视图的布局
        View view =  LayoutInflater.from(context).inflate(R.layout.imageview_upload,this,true);
        rl_img1 = (RelativeLayout)view.findViewById(R.id.rl_img1);
        rl_img2 = (RelativeLayout)view.findViewById(R.id.rl_img2);
        rl_img3 = (RelativeLayout)view.findViewById(R.id.rl_img3);

        iv_img1 = (ImageView)view.findViewById(R.id.iv_img1);
        iv_img2 = (ImageView)view.findViewById(R.id.iv_img2);
        iv_img3 = (ImageView)view.findViewById(R.id.iv_img3);

        iv_clear_img1 = (ImageView)view.findViewById(R.id.iv_clear_img1);
        iv_clear_img2 = (ImageView)view.findViewById(R.id.iv_clear_img2);
        iv_clear_img3 = (ImageView)view.findViewById(R.id.iv_clear_img3);

        iv_add_img1 = (ImageView)view.findViewById(R.id.iv_add_img1);
        iv_add_img2 = (ImageView)view.findViewById(R.id.iv_add_img2);
        iv_add_img3 = (ImageView)view.findViewById(R.id.iv_add_img3);
        iv_clear_img1.setOnClickListener(this);
        iv_clear_img2.setOnClickListener(this);
        iv_clear_img3.setOnClickListener(this);
        iv_add_img1.setOnClickListener(this);
        iv_add_img2.setOnClickListener(this);
        iv_add_img3.setOnClickListener(this);
        if(handler==null){

            handler = new Handler(){
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == 1 && msg.obj != null) {
                        int index = msg.arg1;//处理的是第几张图片 1,2,3
                        int type = msg.arg2;//处理的是增加图片还是减少图片  1为增加 2为减少
                        Bitmap bitmap = (Bitmap)msg.obj;
                        if(index==PIC_ONE&&type==TYPE_ADD){//增加第一张
                            if(bitmapList.size()>=1){
                                bitmapList.remove(0);
                            }
                            bitmapList.add(0,bitmap);
                            iv_img1.setImageBitmap(bitmap);
                            iv_add_img1.setVisibility(View.GONE);
                        }else if(index==PIC_ONE&&type==TYPE_DELETE){//减少第一张
                            bitmapList.remove(0);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==1){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==0){
                                iv_add_img1.setVisibility(View.VISIBLE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }else if(index==PIC_TWO&&type==TYPE_ADD){//增加第二张
                            if(bitmapList.size()==0){
                                bitmapList.add(bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            } else if(bitmapList.size()==1){
                                bitmapList.add(bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==2){
                                bitmapList.remove(1);
                                bitmapList.add(1,bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==3){
                                bitmapList.remove(1);
                                bitmapList.add(1,bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }
                        }else if(index==PIC_TWO&&type==TYPE_DELETE){//减少第二张
                            bitmapList.remove(1);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==1){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }else if(index==PIC_THREE&&type==TYPE_ADD){//增加第三张
                            if(bitmapList.size()==0){
                                bitmapList.add(bitmap);
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.VISIBLE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            } else if(bitmapList.size()==1){
                                bitmapList.add(bitmap);
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }else if(bitmapList.size()==2){
                                bitmapList.remove(2);
                                bitmapList.add(2,bitmap);
                                iv_img3.setImageBitmap(bitmapList.get(2));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }else if(bitmapList.size()==3){
                                bitmapList.remove(2);
                                bitmapList.add(2,bitmap);
                                iv_img3.setImageBitmap(bitmapList.get(2));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.GONE);
                            }
                        }else if(index==PIC_THREE&&type==TYPE_DELETE){//减少第三张
                            bitmapList.remove(2);
                            if(bitmapList.size()==2){
                                iv_img1.setImageBitmap(bitmapList.get(0));
                                iv_img2.setImageBitmap(bitmapList.get(1));
                                iv_add_img1.setVisibility(View.GONE);
                                iv_add_img2.setVisibility(View.GONE);
                                iv_add_img3.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            };

        }

    }

    @Override
    public void onClick(View view) {
        if(view.getId()==iv_add_img1.getId()){
            if(onPicAddAndDel!=null){
                onPicAddAndDel.handleSelectPicture(PIC_ONE,TYPE_ADD);
            }
            index = PIC_ONE;
            type = TYPE_ADD;
        }else if(view.getId()==iv_add_img2.getId()){
            index = PIC_TWO;
            type = TYPE_ADD;
            if(onPicAddAndDel!=null){
                onPicAddAndDel.handleSelectPicture(PIC_TWO,TYPE_ADD);
            }
        }else if(view.getId()==iv_add_img3.getId()){
            index = PIC_THREE;
            type = TYPE_ADD;
            if(onPicAddAndDel!=null){
                onPicAddAndDel.handleSelectPicture(PIC_THREE,TYPE_ADD);
            }
        }else if(view.getId()==iv_clear_img1.getId()){
            index = PIC_ONE;
            type = TYPE_DELETE;
            if(onPicAddAndDel!=null){
                onPicAddAndDel.delPicture(PIC_ONE,TYPE_DELETE);
            }
        }else if(view.getId()==iv_clear_img2.getId()){
            index = PIC_TWO;
            type = TYPE_DELETE;
            if(onPicAddAndDel!=null){
                onPicAddAndDel.delPicture(PIC_TWO,TYPE_DELETE);
            }
        }else if(view.getId()==iv_clear_img3.getId()){
            index = PIC_THREE;
            type = TYPE_DELETE;
            if(onPicAddAndDel!=null){
                onPicAddAndDel.delPicture(PIC_THREE,TYPE_DELETE);
            }
        }
    }

    public interface  OnPicAddAndDel{
        void handleSelectPicture(int index, int type);
        void delPicture(int index, int type);
    }


    public void setOnPicAddAndDel(OnPicAddAndDel onPicAddAndDel) {
        this.onPicAddAndDel = onPicAddAndDel;
    }
}
